package org.example.seata.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.example.seata.dao.OrderDao;
import org.example.seata.domain.Order;
import org.example.seata.service.AccountService;
import org.example.seata.service.OrderService;
import org.example.seata.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private AccountService accountService;

    @Autowired
    private StorageService storageService;

    @Override
    @GlobalTransactional(name = "test-create-order", rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("----->开始新建订单");
        orderDao.create(order);
        log.info("----->根据订单数量扣减库存");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("----->扣减账户余额");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("----->修改订单状态");
        //修改订单状态从0到1 1表示已完成
        orderDao.update(order.getUserId(),0);
        log.info("----->修改订单状态结束");
    }
}
